<template>
  <el-container>
    <el-header>
      <div class="header-left">
        <img src="../assets/logo.png" alt="电商管理平台">
        <span>电商管理平台</span>
      </div>
      <el-button type="primary" @click="logout">注销</el-button>
    </el-header>
    <el-container>
      <el-aside :width="menuToggle ? '64px' : '200px'">
        <el-button class="menu-toggle" @click="menuCallapse">|||</el-button>
        <el-menu background-color="#555" text-color="#fff" active-text-color="#409EFF" :unique-opened="true" :collapse="menuToggle"
          :collapse-transition="false" :router="true" :default-active="currentPath">
          <!-- unique-opened 控制同时只能展开一个菜单,但是不生效,待解决 -->
          <!-- 解决el-menu添加属性不生效问题,原因for循环不能放在其中,否则无法生效 -->
          <el-submenu :index="item.path" v-for="(item,index) in menuList" :key="item.id">
            <template slot="title">
              <i :class="fontIconList[index]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="subItem.path" v-for="(subItem) in item.children" :key="subItem.id" @click="saveCurrentPath(subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
  export default {
    data() {
      return {
        menuList: [],
        menuToggle: true,
        // 保存被激活的菜单
        currentPath: '',
        fontIconList: ['fa fa-users', 'fa fa-plus', 'fa fa-shopping-cart', 'fa fa-reorder', 'fa fa-area-chart']
      }
    },
    created() {
      this.getMenus()
      this.getCurrentPath()
    },
    methods: {
      // 实现退出登录功能
      logout() {
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      // async await 可以简化对返回的 promise数据的操作
      async getMenus() {
        const menus = await this.$http.get('menus')
        // console.log(menus)
        if (!menus) return this.$message.error(menus.meta.msg)
        this.menuList = menus.data
      },
      menuCallapse() {
        this.menuToggle = !this.menuToggle
        // console.log(this.menuToggle)
      },
      saveCurrentPath(path) {
        window.sessionStorage.setItem('currentPath', path)
      },
      getCurrentPath() {
        this.currentPath = window.sessionStorage.getItem('currentPath')
      }
    }
  }
</script>
<style lang='less' scoped>
  .el-container {
    padding: 0;
    height: 100%;
    background-color: #666;

    .el-header {
      display: flex;
      justify-content: space-between;
      justify-items: center;

      .header-left {
        img {
          height: 60px;
          float: left;
        }

        span {
          font-size: 30px;
          font-family: "KaiTi_GB2312";
          color: #fff;
          line-height: 60px;
          float: left;
          padding: 0;
          margin: 0 0 0 20px;
        }
      }

      .el-button {
        height: 40px;
        margin-top: 10px;
        justify-items: center;
      }
    }
  }

  .el-aside {
    background-color: #555;

    .fa {
      margin-right: 8px;
    }

    .el-col {
      width: 100%;
    }

    .el-menu {
      border-right: 0;
    }
  }

  .el-main {
    background-color: #eee;
  }
</style>
